#define _CRT_SECURE_NO_WARNINGS 1
/**
*      author: Y_Yao
*      created: 2024-11-03 11:16:24
**/

#include<bits/stdc++.h>

using namespace std;
using LL = long long;
const LL LLINF = 1e18;
const int INF = 0x3f3f3f3f;
const int N = 1e3 + 7;
int n, m, ret;
char a[N][N];

void solve()
{
    cin >> n >> m;
    for (int i = 1;i <= n;i++)
    {
        for (int j = 1;j <= m;j++)
        {
            cin >> a[i][j];
        }
    }
    ret = 0;
    int cnt = min(n, m) / 2;


    vector<int> t;
    for (int i = 1;i <= cnt;i++)
    {
        int rn = (n - 2 * (i - 1));
        int cn = (m - 2 * (i - 1));
        int x = i, y = i;
        for (int j = 1;j < cn;j++)
        {
            t.push_back(a[x][y++] - '0');
        }
        for (int j = 1;j < rn;j++)
        {
            t.push_back(a[x++][y] - '0');
        }
        for (int j = 1;j < cn;j++)
        {
            t.push_back(a[x][y--] - '0');
        }
        for (int j = 1;j < rn;j++)
        {
            t.push_back(a[x--][y] - '0');
        }

        int tn = t.size();
        for (int j = 0;j < t.size();j++)
        {
            if (t[j] == 1 && t[(j + 1) % tn] == 5 && t[(j + 2) % tn] == 4
                && t[(j + 3) % tn] == 3)
            {
                ret++;
            }
        }
        t.clear();
    }

    cout << ret << endl;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int t = 1;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}